// work/pages/message/message.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msg: 12, //消息
    tabList: [{
      label: '未读',
      value: '0',
      list: []
    }, {
      label: '已读',
      value: '1',
      list: []
    }],
    isMusk: false, //遮罩
    active: 0, //tabs
  },

  listAllNotice() {
    var url = app.globalData.url + 'main/listAllNotice',
      that = this,
      tabList = [{
        label: '未读',
        value: '0',
        list: []
      }, {
        label: '已读',
        value: '1',
        list: []
      }];
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
      memberId: app.globalData.memberId,
      flag: 1
    }, res => {
      if (res.flag) {
        console.log(res);


        if (res.data.readList.length == 0) {
          tabList[0].list = res.data.noticeList
        } else {
          for (var i in res.data.noticeList) {
            res.data.noticeList[i].flag = false;
            for (var j in res.data.readList) {
              if (res.data.noticeList[i].noticeId == res.data.readList[j].noticeId) {
                res.data.noticeList[i].flag = true
              }
            }
          }
          for (var i in res.data.noticeList) {
            if (res.data.noticeList[i].flag) {
              tabList[1].list.push(res.data.noticeList[i])
            } else {
              tabList[0].list.push(res.data.noticeList[i])
            }
          }
        }
        console.log(tabList);
        tabList[0].list = tabList[0].list.sort((a, b) => {
          return new Date(b.noticeTime.replace(/-/g, "/")).getTime() - new Date(a.noticeTime.replace(/-/g, "/")).getTime()
        })
        tabList[1].list = tabList[1].list.sort((a, b) => {
          return new Date(b.noticeTime.replace(/-/g, "/")).getTime() - new Date(a.noticeTime.replace(/-/g, "/")).getTime()
        })
        that.setData({
          tabList
        })
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })
  },
  onChange(e) {
    console.log(e);
    this.setData({
      active: e.detail.index
    })
  },

  toDetail(e) {
    console.log(e);
    var noticeId = e.currentTarget.dataset.flag.noticeId;
    wx.setStorageSync('noticeId', noticeId)
    wx.navigateTo({
      url: '/work/pages/message_detail/message_detail',
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.listAllNotice()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.listAllNotice()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})